<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Cert extends MY_Admin_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('Cert_model');
    }
	
    //默认执行index
	public function index()
	{
            $type = $this->input->get_post('type');
            $status = $this->input->get_post('status');
            $field = $this->input->get_post('field');
            $cKey = $this->input->get_post('txtKey');
            $fieldDate = $this->input->get_post('fieldDate');
            $begtime = $this->input->get_post('begtime');
            $endtime = $this->input->get_post('endtime');
            $orderby = $this->input->get_post('orderby');


            $page     = _get_page();
            $pagesize = 10;
            $arrParam = array();
            $arrWhere = array();
            // 类型
            $arrParam['type'] = !empty($type) ? $type : 1;
            $arrWhere['type'] = !empty($type) ? $type : 1;
                   
            if($status)
            {
                $arrParam['status'] = $status;
                $arrWhere['status'] = $status;
            }

            if($cKey)
            {
                $arrParam['key'] = $cKey;
                if($field=='userid')
                    $arrWhere[$field] = $cKey;
                else
                    $arrWhere[$field.' like '] = "'%$cKey%'";
            }
            $arrParam['field'] = $field;
            $arrParam['fieldDate'] = $fieldDate;

            if($begtime)
            {
                $arrParam['begtime'] = $begtime;
                $arrWhere["$fieldDate >="] = strtotime($begtime);
            }
            if($endtime)
            {
                $arrParam['endtime'] = $endtime;
                $arrWhere["$fieldDate <="] = strtotime("$endtime 23:59:59");
            }
            $strOrder = 'addtime desc';
            if($orderby)
            {
                $arrParam['orderby'] = $orderby;
                $strOrder = $orderby;
            }

            $list = $this->Cert_model->fetch_page($page, $pagesize, $arrWhere,'*', $strOrder);
//             echo $this->Cert_model->db->last_query();die;


            //分页
            $pagecfg = array();
            $pagecfg['base_url']     = _create_url('admin/cert', $arrParam);
            $pagecfg['total_rows']   = $list['count'];
            $pagecfg['cur_page'] = $page;
            $pagecfg['per_page'] = $pagesize;
            //$this->load->library('pagination');
            $this->pagination->initialize($pagecfg);
            $list['pages'] = $this->pagination->create_links();

            $result = array(
                    'list' => $list,
                    'arrParam' => $arrParam,
                    'type' => _get_config('usertype')
                    );


            $this->load->view('admin/cert',$result);
	}

	public function add()
	{
		//需要修改
		$userid	= _get_key_val($this->input->get('userid'), TRUE);
		$result = array();
		$info = array();

		if(!empty($userid))
		{
                    $this->load->model('Userdetail_model');
                    $info = $this->Cert_model->get_info_by_id($userid);
                    $oDetail = $this->Userdetail_model->get_info_by_userid($userid);
                    $info = array_merge($oDetail, $info);
		}

		$oSysModelarea = _get_config('modelarea');
        
        // 风格
            $this->load->model('Style_model');
            $oSysModelstyle = $this->Style_model->getShowStyle();
        //$oSysModelstyle = _get_config('modelstyle');
            $result = array(
                'info'=>$info,
                'oSysModelarea'=>$oSysModelarea,
                'oSysModelstyle'=>$oSysModelstyle,
                );

		$this->load->view('admin/cert_add', $result);
	}

	public function save()
	{
		
		if ($this->input->is_post())
		{
			//验证规则
			$config = array(
				
// 				array(
//                      'field'   => 'realname', 
//                      'label'   => '真实姓名', 
//                      'rules'   => 'trim|required'
//                   ),  
//                 array(
//                      'field'   => 'idno', 
//                      'label'   => '身份证', 
//                      'rules'   => 'trim|required'
//                   ),                
                array(
                     'field'   => 'status', 
                     'label'   => '认证状态', 
                     'rules'   => 'trim|required'
                  ),  
               
               
            );

            $this->form_validation->set_rules($config);

			if ($this->form_validation->run() === TRUE)
  			{
  				//将需要保存的数据赋值给数组$data
  				$data = array(
					
// 					'realname'=>$this->input->post('realname'),
// 					'idno'=>$this->input->post('idno'),
// 					'mobile'=>$this->input->post('mobile'),
// 					'idnoimg'=>$this->input->post('idnoimg'),
// 					'company'=>$this->input->post('company'),
					'bail'=>$this->input->post('bail'),
					'status'=>$this->input->post('status'),
					'op_userid'=>$this->session->userdata['admin_id'],
					'op_username'=>$this->session->userdata['user_name'],
					'op_time'=>time(),
				);

  				$userid	= _get_key_val($this->input->get('userid'), TRUE);
  				//保存至数据库
  				$this->Cert_model->update_by_id($userid,$data);

  				$style = '';
  				if(is_array($this->input->post('style')))
  					$style = implode(',', $this->input->post('style'));
  				$data_detail = array(
					'area'=>$this->input->post('area'),
					'style'=>$style,
					);
  				$this->load->model('Userdetail_model');
  				$this->Userdetail_model->update_by_id($userid,$data_detail);

				//echo '成功,<a href="/admin/aa">返回列表页</a>';
				redirect(base_url('/admin/cert'));
				exit;
  			}
  			else
  			{
  				$userid	= _get_key_val($this->input->get('userid'), TRUE);
				$result = array();
				$info = array();
		
				if(!empty($userid))
				{
		                    $this->load->model('Userdetail_model');
		                    $info = $this->Cert_model->get_info_by_id($userid);
		                    $oDetail = $this->Userdetail_model->get_info_by_userid($userid);
		                    $info = array_merge($oDetail, $info);
				}
		
				$oSysModelarea = _get_config('modelarea');
		        
		        // 风格
	            $this->load->model('Style_model');
	            $oSysModelstyle = $this->Style_model->getShowStyle();
	            $result = array(
	                'info'=>$info,
	                'oSysModelarea'=>$oSysModelarea,
	                'oSysModelstyle'=>$oSysModelstyle,
	                );
  				$this->load->view('admin/cert_add',$result);
  				//redirect('/admin/link/add?id='.$this->input->get('id'));
  			}
			



		}
	}

	function del(){
		$userid	= _get_key_val($this->input->get('userid'), TRUE);
		$page = _get_page();

		$this->Cert_model->delete_by_id($userid);
		redirect( base_url('/admin/cert?page='.$page) );

	}
}
